﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Common;
using CPETwit.WPFClient.Helper;

namespace CPETwit.WPFClient.ViewModel
{
    public class RegisterViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private readonly WCFServiceCaller _serviceCaller;
        private string _userName;
        private string _email;
        private string _password;
        private ICommand _registerCommand;

        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged("Password");
            }
        }
        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public ICommand RegisterCommand
        {
            get
            {
                return _registerCommand ?? new RelayCommand(param => Register(),
                                                            param => CanRegister());
            }
        }

        public RegisterViewModel()
        {
            _serviceCaller = CommonData.GetServiceCaller();
        }

        private void Register()
        {
            bool valid = _serviceCaller.IsEmailValid(Email);
            if (!valid)
            {
                MessageHelper.ShowInformationMessage("Enregistrement", MessageHelper.Register__EmailAlreadyUsed);
                return;
            }

            valid = _serviceCaller.IsUserNameValid(UserName);
            if (!valid)
            {
                MessageHelper.ShowInformationMessage("Enregistrement", MessageHelper.Register__UserNameAlreadyUsed);
                return;
            }

            _serviceCaller.RegisterUser(UserName, Password, Email);
            CommonData.LOGGED_USER = _serviceCaller.GetUserByLogin(UserName);
            DialogManager.ChangeBackGround();
            DialogManager.ShowMainMenu();
        }

        private bool CanRegister()
        {
            return !string.IsNullOrEmpty(UserName) && !string.IsNullOrWhiteSpace(UserName) &&
                   !string.IsNullOrEmpty(Password) &&
                   !string.IsNullOrEmpty(Email) && !string.IsNullOrWhiteSpace(Email);
        }
    }
}
